package com.icart.controller.personController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icart.context.HttpUserContext;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;
import com.icart.controller.HomeController;
import com.icart.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;

import java.util.List;


@Controller
@RequestMapping("/person")
public class PersonOrderDetailController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    CommentitemRepository commentitemRepository;
    @Autowired
    CommentRepository commentRepository;
    @Autowired
    CommentpicRepository commentpicRepository;
    @Autowired
    WoinfoRepository woinfoRepository;
    @Autowired
    private RestTemplate template;

    /*订单详情*/
    @RequestMapping("/orderdetail")
    public String orderdetail(@RequestParam(name = "oid",required = false) String oid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        /*订单详情*/
        String sql="select o.stat,(select m.dictname from sa_db_dictitem m where m.groupid='WO_ORDERSTAT' and m.dictid=o.stat)ordertype,o.cometime," +
                "o.oid,(select f.staffname from bi_chnstaff f where f.chnid=o.chnid and f.staffid=o.servman)staffname," +
                "(select n.chnname from bi_chn n where n.chnid=o.chnid)chnname,o.planfee,o.realfee,o.tcdisc,o.tckdisc " +
                " from wo_info o where o.custid="+userContext.getCustid()+" and o.oid='"+oid+"'";
        List list=commentitemRepository.createNativeQuery(sql);
        model.addAttribute("orderdetail",list.get(0));

        /*订单的服务项目*/
        String sql2="select v.servitemid,(select m.servitemname from bi_servitem m where m.servitemid=v.servitemid and m.chnid=v.chnid)servitemname," +
                "v.wtfee " +
                " from wo_infoserv v where v.custid="+userContext.getCustid()+" and v.oid='"+oid+"'";
        List list2=commentitemRepository.createNativeQuery(sql2);
        model.addAttribute("orderservices",list2);

        /*订单的材料*/
        String sql3="select e.cid,(select o.mname from bi_metinfo o where o.chnid=e.chnid and o.cid=e.cid)mname,e.saleamount,e.metecount " +
                "from wo_infomete e where e.custid="+userContext.getCustid()+" and e.oid='"+oid+"'";
        List list3=commentitemRepository.createNativeQuery(sql3);
        model.addAttribute("ordermetinfo",list3);


        return "person/order/orderdetail";
    }

    /*待评论订单详情*/
    @RequestMapping("/commentorderdetail")
    public String commentorderdetail(@RequestParam(name = "oid",required = false) String oid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        /*项目分类评价*/
        String sql="select * from wo_commentdef";
        List list=commentitemRepository.createNativeQuery(sql);
        model.addAttribute("itemtype",list);

        /*查出订单信息*/
        String sql2="select o.oid,o.servman,o.chnid,(select f.staffname from bi_chnstaff f where f.chnid=o.chnid and f.staffid=o.servman)staffname,o.cometime " +
                " from wo_info o where o.custid="+userContext.getCustid()+" and o.oid='"+oid+"'";
        List list2=commentitemRepository.createNativeQuery(sql2);
        model.addAttribute("order",list2.get(0));

//        /*获取微信的token*/
//        String url="http://pay.wechat.ikartek.com/jssdk/api.php?url=http://testwx.ikartek.com/person/commentorderdetail&token=";
//        HomeController.gettoken(model,template,url,request);

        return "person/order/commentorderdetail";
    }

    /*保存待评论订单详情*/
    @RequestMapping("/savecomment")
    @ResponseBody
    public String commentorderdetail(Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        String item=request.getParameter("item");
        JSONObject jsonObject= JSON.parseObject(item);
        System.out.println(jsonObject);
        JSONArray itemid=jsonObject.getJSONArray("itemid");
        JSONArray num=jsonObject.getJSONArray("num");
        String oid=jsonObject.getString("oid");
        String servman=jsonObject.getString("servman");
        String chnid=jsonObject.getString("chnid");
        String allcommentnum=jsonObject.getString("allcommentnum");
        String textarea=jsonObject.getString("textarea");
        String ifnoname=jsonObject.getString("ifnoname");
        JSONArray list=jsonObject.getJSONArray("list");//图片路径列表
        Integer itemid2=null;
        Integer starnum=null;
        Integer flag=null;
        Integer flag2=null;
        Integer flag3=null;
        Integer ispic=0;//评论是否有图片
        if(list.size()!=0){
            ispic=1;
        }
        Date date=new Date();
        Long commid=date.getTime();//系统时间，暂时作为评论id
        for(int i=0;i<itemid.size();i++){
            itemid2=Integer.parseInt(itemid.get(i).toString());
            starnum=Integer.parseInt(num.get(i).toString());
            if(itemid2!=3){
                flag=commentitemRepository.insert(commid,itemid2,oid,Long.valueOf(servman),starnum);
            }else if(itemid2==3){
                flag=commentitemRepository.insert(commid,itemid2,oid,Long.valueOf(chnid),starnum);
            }
        }
        flag2=commentRepository.insert(commid,oid,userContext.getCustid(),Integer.parseInt(allcommentnum),textarea,date,Integer.parseInt(ifnoname),1,1,ispic);
        flag3=woinfoRepository.update(oid);
        for (int i=0;i<list.size();i++){
            String picid="P"+i;
            commentpicRepository.insert(commid,picid,list.get(i).toString());
        }        if(flag==1&&flag2==1){
            return "success";
        }else {
            return "fail";
        }

    }
}